package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/vieworder")
public class ViewOrderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        HttpSession session = context.getRequest().getSession();

    AccountActionBean accountBean = (AccountActionBean) session.getAttribute("accountBean");

    order = orderService.getOrder(order.getOrderId());

    if (accountBean.getAccount().getUsername().equals(order.getUsername())) {
      return new ForwardResolution(VIEW_ORDER);
    } else {
      order = null;
      setMessage("You may only view your own orders.");
      return new ForwardResolution(ERROR);
    }
         */
        Order order = new Order();
        String orderId = req.getParameter("orderId");
        OrderService orderService = new OrderService();
        Account account = (Account) req.getSession().getAttribute("account");
        order = orderService.getOrder(Integer.parseInt(orderId));
        if (account.getUsername().equals(order.getUsername())) {
            req.getSession().setAttribute("order",order);
            req.getRequestDispatcher("/WEB-INF/jsp/order/ViewOrder.jsp").forward(req,resp);
        } else {
            order = null;
//            setMessage("You may only view your own orders.");
//            return new ForwardResolution(ERROR);
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
